﻿using System;
using System.Collections.Generic;
using System.Globalization;
using YGD.Generic;

namespace YGD.MicroBlogAnalysis
{

    public class TaskBase
    {
        public string Content;
        public List<string> UserNames;
        public string TimingString;
        public DateTime Timing;
        public bool IsTimingTask = false;
    }

    public class MicroBlogAnalysis
    {
        public string contentRegex = @"[^@]+(?=@)";
        public string userNameRegex = @"(@\w+\b+)";
        public string timingRegex = @"(?<=@\w+\b+\s+)[^@]+";


        public TaskBase AnalysisTask(string taskText)
        {
            return AnalysisTask(taskText, DateTime.Today);
        }


        public TaskBase AnalysisTask(string taskText,DateTime createOn)
        {
            var taskBase
                = new TaskBase();
            taskBase.Content = taskText.GetRegexMatch(contentRegex);
            taskBase.UserNames = taskText.GetRegexMatchCollection(userNameRegex);
            taskBase.TimingString = taskText.GetRegexMatch(timingRegex);

            if (AnalysisString(taskBase.TimingString,createOn.Date, out taskBase.Timing))
                taskBase.IsTimingTask = true;
            return taskBase;
        }




        private bool AnalysisString(string timingString,DateTime baseDateTime, out DateTime timing)
        {
            
            try
            {

                var chineseNumberStrings = new Dictionary<string, int> {{ "十一", 11 }, { "十二", 12 }, { "一", 1 }, { "二", 2 }, { "三", 3 }, { "四", 4 }, { "五", 5 }, { "六", 6 }, { "七", 7 }, { "八", 8 }, { "九", 9 }, { "十", 10 } };
                string dayString, timeString;
                timing = baseDateTime.Date;
                timingString = timingString.Trim();
                if (string.IsNullOrEmpty(timingString) || timingString.Length < 2)
                    return false;

                //处理日期
                dayString = timingString.Substring(0, 2);
                if (dayString == "今天") timing = DateTime.Today;
                else if (dayString == "明天") timing = DateTime.Today.AddDays(1);
                else if (dayString == "后天") timing = DateTime.Today.AddDays(2);
                else if (dayString == "周日") timing = DateTime.Today.GetSunday();
                else if (dayString == "周一") timing = DateTime.Today.GetSunday().AddDays(1);
                else if (dayString == "周二") timing = DateTime.Today.GetSunday().AddDays(2);
                else if (dayString == "周三") timing = DateTime.Today.GetSunday().AddDays(3);
                else if (dayString == "周四") timing = DateTime.Today.GetSunday().AddDays(4);
                else if (dayString == "周五") timing = DateTime.Today.GetSunday().AddDays(5);
                else if (dayString == "周六") timing = DateTime.Today.GetSunday().AddDays(6);

                //查找yyyy-MM-dd或者MM-dd
                dayString = timingString.GetRegexMatch(@"(\d{2,4}[-./]\d{1,2}[-./]\d{1,2})|(\d{1,2}[-./]\d{1,2})");
                if (!string.IsNullOrEmpty(dayString))
                {
                    try
                    {
                        var date = Convert.ToDateTime(dayString.Replace('.', '-').Replace('\\', '-').Replace('/', '-'));
                        timing = date;
                    }
                    catch
                    {
                    }
                }

                //处理时间 hh:mm
                timeString = timingString.GetRegexMatch(@"\d{1,2}:\d{2}");

                TimeSpan timeSpan;
                TimeSpan.TryParseExact(timeString, "g", CultureInfo.CurrentCulture, out timeSpan);
                timing = timing.Add(timeSpan);

                //处理时间 下午五点(半)
                foreach (var key in chineseNumberStrings.Keys)
                {
                     if(timingString.Contains(key))
                     {
                         timing = timing.AddHours(chineseNumberStrings[key]);
                         continue;
                     }
                }

                string time = timingString.GetRegexMatch(@"\d{1,2}(?=点)");
                int value;
                if(int.TryParse(time,out value))
                {
                    timing = timing.AddHours(value);
                }

                if (timingString.Contains("半"))
                {
                    timing = timing.AddMinutes(30);
                }


                if (timingString.Contains("下午"))
                {
                    timing = timing.AddHours(12);
                }

                return true;
            }
            catch (Exception)
            {
                timing = DateTime.MinValue;
                return false;
            }
        }
    }

}
